﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CB_TDPT : UserControl
    {
        string temp = "";
        string temp1 = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CB_TDPT()
        {
            InitializeComponent();
        }

        private void dataGridView_CB_TDPT_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           cmb_MaCB_TDPT.Text = dataGridView_CB_TDPT.CurrentRow.Cells[0].Value.ToString();
           cmb_MaTDPT_TDPT.Text = dataGridView_CB_TDPT.CurrentRow.Cells[1].Value.ToString();
           temp = dataGridView_CB_TDPT.CurrentRow.Cells[0].Value.ToString();
           temp1 = dataGridView_CB_TDPT.CurrentRow.Cells[1].Value.ToString();
           cmb_MaCB_TDPT.Enabled = false;
           cmb_MaTDPT_TDPT.Enabled = false;
           btb_Xoa.Enabled = true;
        }

        private void btn_ThemMaTDPT_TDPT_Click(object sender, EventArgs e)
        {
            Frm_Them_TDPT them_tdpt = new Frm_Them_TDPT();
            them_tdpt.ShowDialog();
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void UC_CB_TDPT_Load(object sender, EventArgs e)
        {
            LoadCB_TDPT();
        }
        public void LoadCB_TDPT()
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            dataGridView_CB_TDPT.DataSource = sv.getCB_TDPT();

            cmb_MaCB_TDPT.DataSource = sv.getCB_TDPT();
            cmb_MaCB_TDPT.DisplayMember = "Ma_CB";
            cmb_MaCB_TDPT.ValueMember = "Ma_CB";

            cmb_MaTDPT_TDPT.DataSource = sv.getTDPT();
            cmb_MaTDPT_TDPT.DisplayMember = "TenTDPT";
            cmb_MaTDPT_TDPT.ValueMember = "Ma_TDPT";
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = "them";
            btb_Xoa.Enabled = false;
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            cmb_MaCB_TDPT.Enabled = true;
            cmb_MaTDPT_TDPT.Enabled = true;
            cmb_MaCB_TDPT.Text = "";
            cmb_MaTDPT_TDPT.Text = "";
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_TDPT cbtdpt = new ServiceReference1.CANBO_TDPT();
            string a = cmb_MaCB_TDPT.Text;
            string b = cmb_MaTDPT_TDPT.SelectedValue.ToString();

            cbtdpt.Ma_CB = a;
            cbtdpt.Ma_TDPT = b;
            if (a=="" | b=="")
            {
                MessageBox.Show("Bạn chưa nhập dữ liệu!");
            }
            else
            {
                if (flag == "them")
                {
                    int kq = 0;
                    if (sv.checkCB_TDPT(cbtdpt))
                        kq = -1;
                    else
                        sv.addCB_TDPT(cbtdpt);
                    if (kq != -1)
                    {
                        MessageBox.Show("Hoàn tất!");
                        LoadCB_TDPT();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi!!!");
                    }
                }   
            }
            
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Luu.Enabled = false;
            btb_Xoa.Enabled = true;
        }

        private void btb_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_TDPT cbtdpt = new ServiceReference1.CANBO_TDPT();
            cbtdpt.Ma_CB = temp;
            cbtdpt.Ma_TDPT = temp1;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if(dr==DialogResult.OK)
            {
                sv.deleteCB_TDPT(cbtdpt);
                LoadCB_TDPT();
                MessageBox.Show("Đã xóa");
            }
        }

    }
}
